/*
 * @Author: liuYang codkly@gmail.com
 * @Date: 2022-12-06 14:54:35
 * @LastEditors: liuYang codkly@gmail.com
 * @LastEditTime: 2022-12-08 11:21:29
 * @FilePath: \coding\nest\blog_alpha\src\userinfo\userinfo.service.ts
 * @Description:
 * 暂无备注
 */
import { Injectable } from '@nestjs/common';
import { UserinfoEntity } from '../userinfo/userinfo.entity';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository, Like, Raw, In } from 'typeorm';
@Injectable()
export class UserinfoService {
  constructor(
    @InjectRepository(UserinfoEntity)
    private usersinfoRepository: Repository<UserinfoEntity>,
  ) {}

  // 列表
  async findAll(query: any): Promise<any> {
    const { keyword, page = 1, limit = 10 } = query;
    const skip = (page - 1) * limit;

    let params = {
      skip,
      take: limit,
    };

    let whereParams = {};

    if (keyword) {
      whereParams = Object.assign(whereParams, {
        name: Like(`%${keyword}%`),
      });
    }

    params = Object.assign(
      //   {
      //     select: ['id', 'name', 'updatedAt', 'status'],
      //   },
      params,
      {
        where: whereParams,
      },
      {
        order: {
          updated_at: 'DESC',
        },
      },
    );

    // const [data, total] = await this.usersinfoRepository.createQueryBuilder("userinfo")
    // .leftJoinAndSelect("userinfo.roles", "join")
    // .addSelect("userinfo.name", "name")
    // .getMany();

    const [data, total] = await this.usersinfoRepository.findAndCount(params);

    return {
      total,
      list: data,
    };
  }
}
